var xmlHttp=createNewXmlHttpRequest();
var currentChapterId='';

function createNewXmlHttpRequest(){
	var xmlHttp;
	try{
		xmlHttp=new XMLHttpRequest();
	}catch(e){}

	if(!xmlHttp){
		alert("Error while creating XMLHttpRequest object");
	}else{
		return xmlHttp;
	}
}

function callServerAskForFriend(friend){
	if(xmlHttp){	
		try{
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
				var url="process.php";
				var params="procXHRAgenda=1&friend="+friend;
				xmlHttp.open("POST", url, true);

				//header settings
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", params.length);
				xmlHttp.setRequestHeader("Connection", "close");

				xmlHttp.onreadystatechange=handleRequestStateChange;
				xmlHttp.send(params);
				return true;
			}
		}catch(e){
			alert(e.toString());
		}
	}
}

//tratarea modificarii starii cererii trimise la server
function handleRequestStateChange(){
	if(xmlHttp.readyState == '1'){
		handleServerLoading();
	}else if(xmlHttp.readyState == '4'){
		if(xmlHttp.status == 200){
			//tratarea raspunsului de la server
			try{
				handleServerResponse();
			}catch(e){
				alert("Error reading the response: "+e.toString());
			}
		}else{
			alert("There was a problem retrieving the data from server: "+xmlHttp.statusText);
		}
	}
}

//tratarea raspunsului de la server
function handleServerResponse(){
	var serverResponse=xmlHttp.responseText.toString();
	var helder=document.getElementById('agendaHelder');
	helder.innerHTML=serverResponse;
}

//tratarea timpului de incarcare
function handleServerLoading(){
	var helder=document.getElementById('agendaHelder');
	helder.innerHTML="<div class='loading' align='center'><div><img src='include/images/loading.gif' alt='loading...'></div><div>Loading...</div></div>";
	//alert("Ok");
}